// pages/info/info.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSelect:false,
    select:true,
    typec:'',
    up:'/pic/三角上黑.png',
    down:'/pic/三角下黑.png',
    choosevalue:0,
    splitarray:[
      {index:0,type:'默认排序',display:true},
      {index:1,type:'最新',display:false},
      {index:2,type:'最热',display:false}],
    rentalarray:[],
      personaltarget:'none'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    wx.request({
      url: 'http://127.0.0.1:8080/rental/rentalByNew',
      method:"GET",
      success(res){
        var data = res.data;
        for(var i=0;i<res.data.length;i++){
          data[i].rentalPic = JSON.parse(data[i].rentalPic)
          data[i].size = JSON.parse(data[i].size)
        }
       that.setData({
          rentalarray:data
       })
        // console.log(res)
      }
    })
    this.setData({
      typec:this.data.splitarray[0].type  
    })
  },
  /*改变种类 */
  changetype(e){
    var value  = this.data.isSelect;
    this.setData({
      choosevalue:0,
      up:'/pic/三角上黑.png',
      down:'/pic/三角下黑.png',
      isSelect:!value,
      select:true
    })
  },
  /*选择种类 */
  choosetype(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    // console.log(index)
    if(index == 0){
      that.onLoad();
    }else if(index == 1){
        //查询最新租房信息
        wx.request({
          url: 'http://127.0.0.1:8080/rental/rentalByNew',
          method:"GET",
          success(res){
            var data = res.data;
            for(var i=0;i<res.data.length;i++){
              data[i].rentalPic = JSON.parse(data[i].rentalPic)
              data[i].size = JSON.parse(data[i].size)
            }
           that.setData({
              rentalarray:data
           })
            // console.log(res)
          }
        })
    }else if(index == 2){
      //查询浏览量最高租房信息
      wx.request({
        url: 'http://127.0.0.1:8080/rental/rentalByHot',
        method:"GET",
        success(res){
          var data = res.data;
          for(var i=0;i<res.data.length;i++){
            data[i].rentalPic = JSON.parse(data[i].rentalPic)
            data[i].size = JSON.parse(data[i].size)
          }
         that.setData({
            rentalarray:data
         })
          // console.log(res)
        }
      })
    }
    var len = this.data.splitarray.length;
    for(var i=0 ;i<len;i++){
      var value = 'splitarray['+i+'].display';
      if(i == index){
        this.setData({
          [value]:true,
          isSelect:false,
          typec:this.data.splitarray[i].type
        })
      }
        else{
          this.setData({
            [value]:false
          })
        }
    }
    
  },
  /*价格升序降序 */
  choosevalue(){
    var that = this;
    var value = this.data.choosevalue
    if(value == 0){
      //查看价格最高
      wx.request({
        url: 'http://127.0.0.1:8080/rental/rentalByHigh',
        method:"GET",
        success(res){
          var data = res.data;
          for(var i=0;i<res.data.length;i++){
            data[i].rentalPic = JSON.parse(data[i].rentalPic)
            data[i].size = JSON.parse(data[i].size)
          }
          that.setData({
              rentalarray:data,
              up:'/pic/三角上.png',
              choosevalue:1
          })
        }
      })
      
    }else if(value == 1){
      //查看价格最低
      wx.request({
        url: 'http://127.0.0.1:8080/rental/rentalByLow',
        method:"GET",
        success(res){
          var data = res.data;
          for(var i=0;i<res.data.length;i++){
            data[i].rentalPic = JSON.parse(data[i].rentalPic)
            data[i].size = JSON.parse(data[i].size)
          }
          that.setData({
              rentalarray:data,
              down:'/pic/三角下.png',
              up:'/pic/三角上黑.png',
              choosevalue:2
          })
        }
      })

    }else{
      //查看价格最高
      wx.request({
        url: 'http://127.0.0.1:8080/rental/rentalByHigh',
        method:"GET",
        success(res){
          var data = res.data;
          for(var i=0;i<res.data.length;i++){
            data[i].rentalPic = JSON.parse(data[i].rentalPic)
            data[i].size = JSON.parse(data[i].size)
          }
          that.setData({
              rentalarray:data,
              up:'/pic/三角上.png',
              down:'/pic/三角下黑.png',
              choosevalue:1
          })
        }
      })
    }
    var value  = this.data.isSelect;
    this.setData({
      select:false,
      isSelect:false
    })
    
  },
  /*选择物品 */
  chooseitem(e){
    // console.log(e)
    var index = e.currentTarget.dataset.index;
    var rentalid = this.data.rentalarray[index].rentalID
    wx.navigateTo({
      url: '/pages/house/house?rentalid='+rentalid,
    })
  },
  showperson(e){
    if(wx.getStorageSync('isuser') == "yes"){
      var index = e.currentTarget.dataset.index;
    var rentalinfo = this.data.rentalarray[index];
    this.setData({
      wxAva:rentalinfo.wxAva,
      userName:rentalinfo.userName,
      contactPhone:rentalinfo.phone,
      personaltarget:false
    })

    }else{
        wx.showToast({
        title: '身份未通过审核',
        duration: 1000,
        icon: "error",
        success: (res) => {},
      })
    }
    
   
  },
  cancelperson(){
    this.setData({
      personaltarget:'none'
    })
  }
})